<?php
	$strSQLSearchCriteria = null;
	$txtProduct = null;
	$txtOwnProductCode = null;
	$txtSupplierProductID = null;
	$cboSupplier = null;
	$cboCategory = null;
	$txtUnitPrice = null;
	$chkPromotionalItem = null;
	$strCategoryList = null;
	$cboResultLimit = PRODUCT_LIST_LIMIT;
	
	if(isset($_REQUEST['txtProduct']))
		$txtProduct = cleanFormInput($_REQUEST['txtProduct']);
	
	if(isset($_REQUEST['txtOwnProductCode']))
		$txtOwnProductCode = cleanFormInput($_REQUEST['txtOwnProductCode']);
	
	if(isset($_REQUEST['txtSupplierProductID']))
		 $txtSupplierProductID = cleanFormInput($_REQUEST['txtSupplierProductID']);
		 
	if(isset($_REQUEST['cboSupplier']))
		 $cboSupplier = cleanFormInput($_REQUEST['cboSupplier']);
		 
	if(isset($_REQUEST['cboCategory']))
		 $cboCategory = cleanFormInput($_REQUEST['cboCategory']);
		 
	if(isset($_REQUEST['txtUnitPrice']))
		 $txtUnitPrice = cleanFormInput($_REQUEST['txtUnitPrice']);
		 
	if(isset($_REQUEST['chkPromotionalItem']))
		 $chkPromotionalItem = cleanFormInput($_REQUEST['chkPromotionalItem']);

	if(isset($_REQUEST['strCategoryList']))
		 $chkPromotionalItem = cleanFormInput($_REQUEST['strCategoryList']);
		 
	$strSQLSearchHeader = "SELECT DISTINCT 
								 tblProduct.ID ProductID
								,tblProduct.Product 			ProductName
								,tblProduct.Description
								,tblProduct.Supplier 			SupplierID
								,tblProduct.Category  			CategoryID
								,tblProduct.QuantityPerUnit 
								,tblProduct.UnitPrice 
								,tblProduct.FullPrice 
								,tblProduct.UnitsInStock 
								,tblProduct.UnitsOnOrder 
								,tblProduct.ReorderLevel 
								,tblProduct.Discontinued
								,tblProduct.OwnProductCode
								,tblProduct.UploadedPicture 	Picture
								,tblProduct.UploadedPicture 	
								,tblCategory.ID					CategoryID
								,tblCategory.Category			CategoryName
								,tblCategory.Description 		\"Desc\"";
	if($txtProduct)
		$strSQLSearchHeader .=" , MATCH (tblProduct.Product, tblProduct.Description, tblProduct.ExtraDescription) AGAINST ('$txtProduct') AS Score ";
									
	$strSQLSearchHeader .= "FROM tblProduct	
							LEFT JOIN tblCategory ON 
								tblProduct.Category = tblCategory.ID 
							LEFT JOIN tblSubCategoryAndProduct
								ON tblSubCategoryAndProduct.Product = tblProduct.ID
							WHERE tblProduct.Discontinued =  '0' ";
	
	$strSQLRecordCount = "SELECT Count(*)	RecordCount
					FROM tblProduct				
					LEFT JOIN tblCategory ON tblProduct.Category = tblCategory.ID
					WHERE tblProduct.Discontinued =  '0' ";
	$strJoin = " AND ";

	

	/*
	 * Need to change this to boolean search (break all the words apart in the search
	 * http://devzone.zend.com/node/view/id/1304
	 */
    if ($txtProduct)
    {
        $strSQLSearchCriteria .= $strJoin."(MATCH (tblProduct.Product, tblProduct.Description, tblProduct.ExtraDescription) AGAINST ('$txtProduct') 
        									OR tblProduct.Product LIKE '".$txtProduct."%' )";
        $strJoin = " AND ";
        //AGAINST (+'$txtProduct'%	 IN BOOLEAN MODE)";
    }

    if ($txtOwnProductCode){
        $strSQLSearchCriteria .= $strJoin."tblProduct.OwnProductCode LIKE '$txtOwnProductCode%'";
        $strJoin = " AND ";
    }	
	
	if ($txtSupplierProductID){
        $strSQLSearchCriteria .= $strJoin."tblProduct.SupplierProductID LIKE '$txtSupplierProductID%'";
        $strJoin = " AND ";
    }

    if ($cboSupplier){
        $strSQLSearchCriteria .= $strJoin."tblProduct.Supplier = '$cboSupplier'";
        $strJoin = " AND ";
    }	 
	
    if ($cboCategory){
        $strSQLSearchCriteria .= $strJoin."tblProduct.Category = '$cboCategory'";
        $strJoin = " AND ";
    }else{
		if ($strCategoryList){
			$strSQLSearchCriteria .= $strJoin."tblProduct.Category IN (".$strCategoryList.")"; 
			$strJoin = " AND ";
			unset($strCategoryList);
		}
	}

    if ($txtUnitPrice){
        $strSQLSearchCriteria .= $strJoin."tblProduct.UnitPrice LIKE '$txtUnitPrice%' ";
        $strJoin = " AND ";
    }

    if ($chkPromotionalItem){
        $strSQLSearchCriteria .= $strJoin."tblProduct.PromotionalItem = '1' ";
        $strJoin = " AND ";
    }
    
    if($_REQUEST['cboSubCategory'] != "")
    {
        $strSQLSearchCriteria .= $strJoin."tblSubCategoryAndProduct.SubCategory = '".mysql_real_escape_string($_REQUEST['cboSubCategory'])."' ";
        $strJoin = " AND ";
    }

// Get Count of records for number of pages to display

$objRecordCount = new MySqlResults($strSQLRecordCount.$strSQLSearchCriteria);
	
	if($objRecordCount->ErrorMessages){
		array_push($ActionResults, $objRecordCount->ErrorMessages);
	}

$_SESSION['sesProductSearchRecord'] = $objRecordCount->myrow["RecordCount"];


// Do search
$strSQL = $strSQLSearchHeader.$strSQLSearchCriteria;
if($txtProduct)
	$strSQL .= " ORDER BY Score Desc";

	if (!isset($strDirection))
	{
		$strDirection = "First";
	}
		
	switch ($strDirection){;
		case "Next";
			$_SESSION['sesFirstLine'] = $_SESSION['sesFirstLine'] + $cboResultLimit;
			break;
			
		Case "Prev";
			if ($_SESSION['sesFirstLine'] - $cboResultLimit >= 0){
				$_SESSION['sesFirstLine'] = $_SESSION['sesFirstLine'] - $cboResultLimit;}
			break;
		
		Case "First";
			$_SESSION['sesFirstLine'] = "0";
			$_SESSION['sesQueryCount'] = 0;
			$_SESSION['sesQueryCount'] = 0;
			$_SESSION['sesLastLine'];
			$_SESSION['sesFirstLine'];
			$_SESSION['sesProductSearchRecord'];
			break;
		
		case "Current";
			break;
		
		case "SelectPage":
			$_SESSION['sesFirstLine'] = ($intSelectedPage * PRODUCT_LIST_LIMIT) - PRODUCT_LIST_LIMIT;
			break;
	}
			
	$_SESSION['sesLastLine']  = $cboResultLimit;
	$_SESSION['sesQueryCount'] ++;
	
	$strSQL .= " LIMIT ".$_SESSION['sesFirstLine'].", ".$_SESSION['sesLastLine'];
	//echo $strSQL;  
    include (MODULE . "/action/actMySqlResults");
	

	//*/
		

?>
